'''
Author: luqinghua
Date: 2025-04-09 15:44:05
LastEditors: luqinghua
LastEditTime: 2025-04-23 11:06:18
FilePath: \python\python\win.py
Description: 工具栏托盘代码示例
'''
import webview
import threading
import pystray
from PIL import Image
import os


def run_pystray(window):
    def show_window(window):
        window.show()
        window.restore()
    def hide_window(window):
        window.hide()
    def setup(icon):
        icon.visible = True
    # 加载图标，这里需要替换为实际的图标文件路径
    # icon_path = os.path.join(os.getcwd(), "rpa.ico")
    # image = Image.open(icon_path)
    image=Image.new("RGB", (128, 128), "#FF0000")
    menu = (
        pystray.MenuItem('显示窗口', lambda: show_window(window)),
        pystray.MenuItem('隐藏窗口', lambda: hide_window(window)),
        pystray.MenuItem('退出', lambda: (window.destroy(), icon.stop()))
    )
    global icon
    icon = pystray.Icon("RPA", image, "RPA", menu)
    # api.set_icon(icon)
    icon.run(setup)

def create_webview():
    global window
    window = webview.create_window(
        title="RPA Client",  # 窗口标题
        url='www.baidu.com',  # 加载的 URL
        min_size=(800, 600),  # 最小尺寸
    )

    def on_minimize():
        if window:
            window.hide()

    window.events.minimized += on_minimize
    return window

if __name__ == '__main__':
    window = create_webview()
    # 启动一个新线程来运行 pystray
    pystray_thread = threading.Thread(target=run_pystray, args=(window,))
    pystray_thread.start()
    # 启动 pywebview 应用
    webview.start()
    
    